/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.component;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;

import com.ruipengkj.bean.TExePos;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;
/**
 * 说明：
 * 菜品按钮
 */
public class RPJButtonDesk extends JToggleButton {

	private static final long serialVersionUID = 1L;
	
	private TExePos tExePos;
	private ImageIcon tableIcon0 = ImageFactory.loadImage(ImageFactory.BTN_TABLE0);
	private ImageIcon tableIcon1 = ImageFactory.loadImage(ImageFactory.BTN_TABLE1);
	private ImageIcon tableIcon2 = ImageFactory.loadImage(ImageFactory.BTN_TABLE2);
	
	private static ButtonGroup bg = new ButtonGroup();
	/**状态1空闲，2用餐，3预订*/
	private int type;
	public RPJButtonDesk(String text, ActionListener action){
		super(text);
		RPJButtonDesk.bg.add(this);
		setContentAreaFilled(false); // 这一句非常重要, 否则父类还会绘制按钮的区域.
//		setFocusPainted(false);
		setBorderPainted(false);
		setCursor(new Cursor(Cursor.HAND_CURSOR));
		addActionListener(action);
		setEnabled(false);//是否可编辑
		setIcon(tableIcon0);
		setHorizontalTextPosition(SwingConstants.CENTER);//水平对齐方式
		setVerticalTextPosition(SwingConstants.BOTTOM);//水平对齐方式
//		setBorder(null);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		if (getModel().isSelected() && isEnabled()) {
			g2d.setColor(Color.red);
			g2d.drawRect(this.getWidth()-23, 6, 13, 13);
			g2d.setFont(Util.FONT_13_BOLD);
			g2d.drawString("√", this.getWidth()-23, 19);
		}
//		Graphics2D g2d = (Graphics2D) g;
//		if (getModel().isRollover()) {
//			//鼠标滑过
//			if (tCommodity != null && tCommodity.getName() != null) {
//				g2d.setColor(Color.white);
//				FontMetrics fm=g2d.getFontMetrics();
//				g2d.drawString(tCommodity.getName(), (float) 117/2-fm.stringWidth(tCommodity.getName())/2, 
//						(float) 57 - 35);
//				g2d.setColor(new Color(255, 211, 96));
//				g2d.drawString("￥"+tCommodity.getDiscountPrice(), (float) 117/2-fm.stringWidth("￥"+tCommodity.getDiscountPrice())/2, 
//						(float) 57 - 15);
//			}
//		} else {
//			if (tCommodity != null && tCommodity.getName() != null) {
//				g2d.setColor(Color.black);
//				FontMetrics fm=g2d.getFontMetrics();
//				g2d.drawString(tCommodity.getName(), (float) 117/2-fm.stringWidth(tCommodity.getName())/2, 
//						(float) 57 - 35);
//				g2d.setColor(Color.white);
//				g2d.drawString("￥"+tCommodity.getDiscountPrice(), (float) 117/2-fm.stringWidth("￥"+tCommodity.getDiscountPrice())/2, 
//						(float) 57 - 15);
//			}
//		}
		
	}
	
	public int getType() {
		return type;
	}

	/**状态1空闲，2用餐，3预订*/
	public void setType(int type) {
		this.type = type;
	}

	public TExePos getTExePos() {
		return tExePos;
	}
	
	public void setTExePos(TExePos tExePos) {
		this.tExePos = tExePos;
		if (tExePos != null && tExePos.getName() != null) {
			this.setText(tExePos.getName());
		
			switch(tExePos.getStatus()) {
			case 0:
			case 1:
				setIcon(tableIcon0);
				break;
			case 2:
				setIcon(tableIcon1);
				break;
			case 3:
				setIcon(tableIcon2);
				break;
			}
		} else {
			setIcon(tableIcon0);
		}
	}
}
